package com.jml.每特.mybatis注解.utils;

import java.util.ArrayList;
import java.util.List;


public class SQLUtils {
	/**
	 * 
	 * 获取Insert语句后面values 参数信息<br>
	 * @param sql
	 * @return
	 */
	public static String[] sqlInsertParameter(String sql) {
		int startIndex = sql.indexOf("values");
		int endIndex = sql.length();
		String substring = sql.substring(startIndex + 6, endIndex);
		substring = substring.replace("(", "");
		substring = substring.replace(")", "");
		substring = substring.replace("#{", "");
		substring = substring.replace("}", "");
		String[] split = substring.split(",");
		return split;
	}

	/**
	 * @param sql
	 * @return
	 */
	public static List<String> sqlSelectParameter(String sql) {
		int startIndex = sql.indexOf("where");
		int endIndex = sql.length();
		String substring = sql.substring(startIndex + 5, endIndex);
		String[] split = substring.split("and");
		List<String> listArr = new ArrayList<>();
		for (String string : split) {
			String[] sp2 = string.split("=");
			listArr.add(sp2[0].trim());
		}
		return listArr;
	}

	/**
	 * 将SQL语句的参数替换变为?<br>
	 * @param sql
	 * @param parameterName
	 * @return
	 */
	public static String parameQuestion(String sql, String[] parameterName) {
		for (int i = 0; i < parameterName.length; i++) {
			String string = parameterName[i];
			sql = sql.replace("#{" + string + "}", "?");
		}
		return sql;
	}

	public static String parameQuestion(String sql, List<String> parameterName) {
		for (int i = 0; i < parameterName.size(); i++) {
			String string = parameterName.get(i);
			sql = sql.replace("#{" + string + "}", "?");
		}
		return sql;
	}

	public static void main(String[] args) {
		 String sql = "insert into user(userName,userAge) values(#{userName1},#{userAge2})";
		 String[] sqlParameter = sqlInsertParameter(sql);
		 for (String string : sqlParameter) {
		 	System.out.println(string);
		 }
		String newSql = parameQuestion(sql, sqlParameter);
		System.out.println(newSql);

		List<String> sqlSelectParameter = SQLUtils
				.sqlSelectParameter("select * from User where userName=#{userName} and userAge=#{userAge} ");
		for (String string : sqlSelectParameter) {
			System.out.println(string);
		}
	}
}
